﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Maple.Weixin.MP.Helpers;

namespace Maple.Weixin.MP.Entities.Messages
{
    public sealed class ResponseMessageNews : ResponseMessageBase
    {
        private readonly List<Article> articleList = new List<Article>(10);

        internal ResponseMessageNews() : this(false) {}

        internal ResponseMessageNews(bool encrypt) : base(encrypt) {}

        public void Add(Article article) {
            articleList.Add(article);
        }

        public void AddRange(IEnumerable<Article> articles) {
            articleList.AddRange(articles);
        }

        public void Remove(Article article) {
            articleList.Remove(article);
        }

        public override ResponseMessageType MessageType
        {
            get { return ResponseMessageType.News; }
        }

        public Article[] Articles { get { return articleList.Take(10).ToArray(); } }

        public int ArticleCount { get { return articleList.Count; } }

        public override XDocument ToXDocumentBase()
        {
            var document = new XDocument(
                new XElement("xml",
                             new XElement("ToUserName", new XCData(ToUserName)),
                             new XElement("FromUserName", new XCData(FromUserName)),
                             new XElement("CreateTime", WeixinHelper.GetWeixinDateTime(CreateTime)),
                             new XElement("MsgType", new XCData(MessageType.ToString().ToLower())),
                             new XElement("ArticleCount", Articles.Length),
                             new XElement("Articles")
                    ));

            foreach (var element in Articles.Select(article => new XElement("item",
                                                                            new XElement("Title", new XCData(article.Title)),
                                                                            new XElement("Description", new XCData(article.Description)),
                                                                            new XElement("PicUrl", new XCData(article.PicUrl)),
                                                                            new XElement("Url", new XCData(article.Url))
                                                                   ))) {
                if (document.Root != null) {
                    var xElement = document.Root.Element("Articles");
                    if (xElement != null)
                        xElement.Add(element);
                }
            }

            return document;
        }
    }

    public sealed class Article
    {
        private string title;
        private string description;
        private string picUrl;
        private string url;

        public string Title {
            get { return title ?? ""; }
            set { title = value; }
        }

        public string Description {
            get { return description ?? ""; }
            set { description = value; }
        }

        public string PicUrl {
            get { return picUrl ?? ""; }
            set { picUrl = value; }
        }

        public string Url {
            get { return url ?? ""; }
            set { url = value; }
        }
    }
}
